package service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.hash.HashMapper;
import service.edu.entity.Comment;
import service.edu.entity.Course;
import service.edu.mapper.CommentMapper;
import service.edu.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import service.edu.service.CourseService;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author diao
 * @since 2020-09-23
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CourseService courseService;

    //分页查询
    @Override
    public HashMap<String, Object> pageQuery(Page<Comment> page,String courseId) {
        Course course = courseService.getById(courseId);
        String teacherId = course.getTeacherId();
        //做添加查询
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("gmt_create");
        wrapper.eq("course_id", courseId);
        wrapper.eq("teacher_id", teacherId);
        //进行分页查询
        baseMapper.selectPage(page,wrapper);
        //获取分页数据
        long current = page.getCurrent();
        long pages = page.getPages();
        List<Comment> commentList = page.getRecords();
        long size = page.getSize();
        long total = page.getTotal();
        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();
        //将分页数据添加至map中
        HashMap<String, Object> map = new HashMap<>();
        map.put("current", current);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("pages",pages);
        map.put("commentList",commentList);
        map.put("hasPrevious", hasPrevious);
        //返回结果
        return map;
    }
}
